package com.jintian.smart.kernel.switching.fn;

import com.jintian.smart.kernel.core.annotation.beetl.BeetlFunction;
import org.beetl.core.Context;
import org.beetl.core.Function;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.Collection;

@BeetlFunction(code = "contains")
public class Contains implements Function {
    @SuppressWarnings("rawtypes")
    @Override
    public Object call(Object[] paras, Context ctx) {
        Object p1 = paras[0];
        Object p2 = paras[1];
        Collection c1 = null;
        if (p1.getClass().isArray()) {
            c1 = Arrays.asList((Object[]) p1);
        } else if (p1 instanceof Collection) {
            c1 = (Collection) p1;
        } else {
            c1 = Arrays.asList(p1);
        }
        Collection c2 = null;
        if (p2.getClass().isArray()) {
            c2 = Arrays.asList((Object[]) p2);
        } else if (p2 instanceof Collection) {
            c2 = (Collection) p2;
        } else {
            c2 = Arrays.asList(p2);
        }
        return CollectionUtils.containsAny(c1, c2);
    }
}
